<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE9" />
<title>演示用-demo</title>

<link rel="stylesheet"
	href="<%=request.getContextPath() %>/framework/amazeui/css/amazeui.min.css">
<link rel="stylesheet"
	href="<%=request.getContextPath() %>/framework/amazeui/css/admin.css">
<link rel="stylesheet"
	href="<%=request.getContextPath() %>/framework/layer/theme/default/layer.css">
</head>
<body>
	<div class="admin-content">
		<div class="admin-content-body">
			<div class="am-cf am-padding am-padding-bottom-0">
				<div class="am-fl am-cf">
					<strong class="am-text-primary am-text-lg">DEMO</strong>
				</div>
			</div>

			<hr>

			<div class="am-g">
				<div class="am-u-sm-12 am-u-md-6">
					<div class="am-btn-toolbar">
						<div class="am-btn-group am-btn-group-xs">
							<button type="button" class="am-btn am-btn-default"
								onclick="downloadTemplate()">
								<span class="am-icon-download"></span> 导出模版
							</button>
							<form id="uploadPDFForm" method="post"
								enctype="multipart/form-data"
								action="<%=request.getContextPath()%>/upload"
								class="am-form-group am-btn-group-xs am-form-file am-fl">
								<button type="button" class="am-btn am-btn-default">
									<span class="am-icon-save"></span> 上传试卷
								</button>
								<input type="file" name="pdfFile" onchange="uploadPDF()"
									accept="application/pdf"></input>
							</form>
							<button type="button" class="am-btn am-btn-default"
								onclick="print();">
								<span class="am-icon-print"></span> 打印试卷
							</button>
						</div>
					</div>
				</div>
				<div class="am-u-sm-12 am-u-md-3">
					<div class="am-form-group"></div>
				</div>
				<div class="am-u-sm-12 am-u-md-3">
					<form method="get" action="<%=request.getContextPath() %>/index"
						id="searchForm">
						<div class="am-input-group am-input-group-sm">
							<span class="am-form-field am-padding-0 am-text-right"
								style="border: none;"> <select
								data-am-selected="{btnSize: 'sm',maxHeight:200}"
								name="majorCode">
									<option value="">所有学科</option>
									<c:forEach items="${majorList }" var="major">
										<option
											<c:if test="${major.majorCode eq majorCode}"> selected="selected"</c:if>
											value="${major.majorCode }">${major.majorName }</option>
									</c:forEach>
							</select>
							</span> <span class="am-input-group-btn">
								<button class="am-btn am-btn-default" type="submit">搜索</button>
								<button class="am-btn am-btn-default" type="button"
									onclick="resetForm();">重置</button>
							</span> <input type="hidden" name="currentPage" value="${page.current }" />
						</div>
					</form>
				</div>
			</div>

			<div class="am-g">
				<div class="am-u-sm-12">
					<form class="am-form">
						<table class="am-table am-table-striped am-table-hover table-main"
							id="mainTable">
							<thead>
								<tr>
									<th><input type="checkbox" /></th>
									<th width="200">学科</th>
									<th width="120">学科代码</th>
									<th width="150">试题状态</th>
									<th>学生列表</th>
								</tr>
							</thead>
							<tbody>
								<c:forEach items="${resultList }" var="r">
									<tr>
										<td><input type="checkbox" value="${r.majorCode }" /></td>
										<td>${r.majorName }</td>
										<td>${r.majorCode }</td>
										<td><c:choose>
												<c:when test="${!empty r.status and r.status eq '1'}">
													<a target="_blank" href="<%=request.getContextPath() %>/download?majorCode=${r.majorCode}" title="下载附件">已上传</a>
													<input type="hidden" name="status" value="1" />	
												</c:when>
												<c:otherwise>
													未上传
													<input type="hidden" name="status" value="0" />
												</c:otherwise>
											</c:choose>
										</td>
										<td><c:forEach items="${r.studentList }" var="student"
												varStatus="v">
												<span style="white-space: nowrap;">
													${student.name}[${student.studentNo}] <c:if
														test="${!v.last}">，</c:if>
												</span>
											</c:forEach></td>
									</tr>
								</c:forEach>
							</tbody>
						</table>
						<div class="am-cf" id="pagination"></div>
					</form>
				</div>

			</div>
		</div>
	</div>
	<%@ include file="selectPrintService.jsp" %>

	<div class="am-modal am-modal-loading am-modal-no-btn am-modal-out"
		tabindex="-1" id="loading-modal">
		<div class="am-modal-dialog">
			<div class="am-modal-hd">正在处理中，请稍候...</div>
			<div class="am-modal-bd">
				<span class="am-icon-spinner am-icon-spin"></span>
			</div>
		</div>
	</div>

	<script src="<%=request.getContextPath() %>/js/jquery.min.js"></script>
	<script src="<%=request.getContextPath() %>/js/jquery.form.js"></script>
	<script
		src="<%=request.getContextPath() %>/framework/amazeui/js/amazeui.js"></script>
	<script
		src="<%=request.getContextPath() %>/framework/amazeui/js/app.js"></script>
	<script src="<%=request.getContextPath() %>/framework/layer/layer.js"></script>

	<script>
	var current = ${page.current};
	var pages = ${page.pages};
	var total = ${page.total};
	var pageSize = ${page.pageSize};
	
	function downloadTemplate(){
		var $checkbox = $("#mainTable tbody>tr>td>input[type='checkbox']:checked");
		if($checkbox.length == 0){
			layer.msg("请至少选择一个学科！", {time: 1500});
			return;
		}
		else{
			var $form = $("<form method='post' target='_blank' action='<%=request.getContextPath() %>/downloadTemplate' class='am-hide'></form>");
			$(document).find("body").append($form);
			
			for(var i = 0;i < $checkbox.length;i++){
				var $majorCode = $("<input name='majorCode' type='hidden' value='"+$checkbox.eq(i).val()+"' />");
				$form.append($majorCode);
			}
			$form[0].submit();
			$form.remove();
		}
	}
	
	function uploadPDF(){
		$("#loading-modal").modal("open");
		var fileName = $('#uploadPDFForm')[0].pdfFile.value;
		var fileType = fileName.substring(fileName.lastIndexOf(".")+1);
		var arr = fileName.split("\\"); 
		var nameView = arr[arr.length-1];
		
		if(fileType!='pdf'){
			layer.msg("请选择pdf格式的件上传!", {time: 1500});
			$("#loading-modal").modal("close");
			return;
		} else{
			$("#uploadPDFForm").ajaxSubmit({
				headers : {"ClientCallMode" : "ajax"},
				success : function(result) {
					$("#loading-modal").modal("close");
					switch(result){
					case "success": layer.msg("上传成功！", {time: 1500});
						list(current);
						$("#uploadPDFForm")[0].reset();
						break;
					case "exists": layer.confirm('系统中已经存在一份该学科的试题，是否选择覆盖？', {
							  btn: ['覆盖','放弃']
						}, function(){
							$("#loading-modal").modal("open");
							$.post("<%=request.getContextPath()%>/upload/cover",{},function(){
								$("#loading-modal").modal("close");
								list(current);
							});
							$("#uploadPDFForm")[0].reset();
						}, function(){
							list(current);
							$("#uploadPDFForm")[0].reset();
						});break;
					case "not_found": layer.msg("无法识别上传文件中的二维码！", {time: 1500});
						$("#uploadPDFForm")[0].reset();
						break;
					case "file_name_error": layer.msg("文件名称与识别出的代码不匹配！", {time: 1500});
						$("#uploadPDFForm")[0].reset();
						break;
					case "error": layer.msg("上传失败，请尝试重新上传！", {time: 1500});
						$("#uploadPDFForm")[0].reset();
						break;
					}
				},
				error : function(){
					$("#loading-modal").modal("close");
					layer.msg("上传失败，请尝试重新上传！", {time: 1500});
					$("#uploadPDFForm")[0].reset();
				},
				dataType: "html",
				clearForm: true,
				timeout: 10 * 1000  
			});
		}
	}
	
	function print(){
		var $checkbox = $("#mainTable tbody>tr>td>input[type='checkbox']:checked");
		if($checkbox.length == 0){
			layer.msg("请至少选择一个学科！", {time: 1500});
			return;
		}
		else{
			for (var i = 0; i < $checkbox.length; i++) {
				var $majorCode = $checkbox.eq(i).parents("tr").eq(0).find("input[name='status']");
				if($majorCode.val() == 0){
					layer.msg("选择的学科中存在未上传试题的学科，请重新选择！", {time: 1500});
					return;
				}
			}

			$("#loading-modal").modal("open");
			var $form = $("<form method='post' target='_blank' action='<%=request.getContextPath() %>/print' class='am-hide'></form>");
			$(document).find("body").append($form);

			for (var i = 0; i < $checkbox.length; i++) {
				var $majorCode = $("<input name='majorCode' type='hidden' value='" + $checkbox.eq(i).val() + "' />");
				$form.append($majorCode);
			}
			$form.ajaxSubmit({
				success : function(result) {
					$("#loading-modal").modal("close");
					switch(result.status){
					case "success": startPrint(result.allPrintService);
						break;
					case "not_exists": layer.msg("选择的学科中存在未上传试题的学科，请重新选择！", {time: 1500});
						break;
					case "error": layer.msg("打印失败，请尝试重新打印！", {time: 1500});
						break;
					}
				},
				error : function(){
					$("#loading-modal").modal("close");
					layer.msg("打印失败，请尝试重新打印！", {time: 1500});
				},
				dataType: "json",
				timeout: 30 * 1000  
			});
			$form.remove();
		}
	}

	function resetForm() {
		var $option = $($('#searchForm')[0].majorCode).find('option');
		$option.prop('selected', false);
		$option.eq(0).prop('selected', true);
		$option.eq(0).trigger('changed.selected.amui');
		$('#searchForm')[0].currentPage.value = 1;
	}

	function list(currentPage) {
		$("#searchForm")[0].currentPage.value = currentPage;
		$("#searchForm")[0].submit();
	}

	function showPage(current, pageSize, size) {
		if (!size) {
			size = defaultPageSize;
		}
		pageSize = pageSize <= 0 ? 1 : pageSize;
		current = current < 1 ? 1 : (current > pageSize ? pageSize : current);

		var str = '共' + pageSize + '页 ' + total + '条记录 当前第' + current + '页 <div class="am-fr">';

		str += '<ul class="am-pagination">';

		if (current <= 1) {
			str += '<li class="am-disabled" title="第一页"><a href="javascript:void(0);">«</a></li>';
			str += '<li class="am-disabled" title="上一页"><a href="javascript:void(0);">‹</a></li>';
		} else {
			str += '<li title="第一页"><a href="javascript:list(' + 1 + ');">«</a></li>';
			str += '<li title="上一页"><a href="javascript:list(' + (current - 1) + ');">‹</a></li>';
		}

		var half = Math.floor(size / 2);
		var startIndex = 0;
		if (current <= half) {
			startIndex = 0;
		} else if (current >= (pageSize - half)) {
			startIndex = pageSize - size;
		} else {
			startIndex = current - half;
			if (size % 2 != 0) {
				startIndex--;
			}
		}
		startIndex = startIndex < 0 ? 0 : startIndex;
		for (var i = startIndex; i < startIndex
				+ (size < pageSize ? size : pageSize); i++) {
			str += i + 1 == current ? 
					'<li class="am-active"><a href="javascript:void(0);">' + (i + 1) + '</a></li>'
					: '<li><a href="javascript:list(' + (i + 1) + ');">' + (i + 1) + '</a></li>';
		}

		if (current >= pageSize) {
			str += '<li class="am-disabled" title="下一页"><a href="javascript:void(0);">›</a></li>';
			str += '<li class="am-disabled" title="最后一页"><a href="javascript:void(0);">»</a></li>';
		} else {
			str += '<li title="下一页"><a href="javascript:list('
					+ (current + 1) + ');">›</a></li>';
			str += '<li title="最后一页"><a href="javascript:list(' + pageSize
					+ ');">»</a></li>';
		}

		str += '</ul></div>';
		$("#pagination").html(str);
	}

	$(function() {
		showPage(current, pages, 7);

		$("#mainTable thead>tr>th>input[type='checkbox']").bind('click',function() {
			var checked = $(this).prop("checked");
			$("#mainTable tbody>tr>td>input[type='checkbox']").prop("checked", checked);
		});

		$("#mainTable tbody>tr").bind('click',function($obj) {
			if($obj.target != $(this).find("td>input[type='checkbox']")[0]){
				var checked = $(this).find("td>input[type='checkbox']").prop("checked");
				$(this).find("td>input[type='checkbox']").prop("checked", !checked);
			}

			if ($("#mainTable tbody>tr>td>input[type='checkbox']:checked").length == $("#mainTable tbody>tr>td>input[type='checkbox']").length) {
				$("#mainTable thead>tr>th>input[type='checkbox']").prop("checked", true);
			} else {
				$("#mainTable thead>tr>th>input[type='checkbox']").prop("checked", false);
			}
		});
	})
	</script>
</body>
</html>